var TableColSetting = function () {
  var template = /* html */ `
    <div class="table-col-setting">
      <div class="table-col-setting-title">颜色物料BOM</div>
      <div class="table-col-setting-content">
        <check-group :data="materialColorBomColList" :size="6"></check-group>
      </div>
      <div class="table-col-setting-title">物料BOM</div>
      <div class="table-col-setting-content">
        <check-group :data="materialBomColList" :size="6"></check-group>
      </div>
      <div class="table-col-setting-title">辅料BOM</div>
      <div class="table-col-setting-content">
        <check-group :data="subsidiaryBomColList" :size="6"></check-group>
      </div>
      <div style="text-align:center;margin-top:25px">
        <el-button size="medium" type="primary" @click="ensure">确定</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      config: {
        type: Object,
        required: true
      }
    },

    data: function () {
      return {
        materialColorBomColList: [],
        materialBomColList: [],
        subsidiaryBomColList: []
      };
    },

    created: function () {
      this.setTableConfigData();
    },

    methods: {
      setTableConfigData: function () {
        var config = this.config;
        var tableCol = BomMaterialFormModel.tableCol;
        var materialColorBomColList = tableCol.materialColorBomCol;
        materialColorBomColList = _.map(materialColorBomColList, function (obj) {
          var id = obj.id;
          var checked = config.materialColorBomCol.indexOf(id) === -1;
          return {
            id: id,
            label: obj.label,
            checked: checked
          };
        });
        this.setClassifyConfig(materialColorBomColList, config.materialColorBomCol);
        this.materialColorBomColList = materialColorBomColList;

        var materialBomColList = tableCol.materialBomCol;
        materialBomColList = _.map(materialBomColList, function (obj) {
          var id = obj.id;
          var checked = config.materialBomCol.indexOf(id) === -1;
          return {
            id: id,
            label: obj.label,
            checked: checked
          };
        });
        this.setClassifyConfig(materialBomColList, config.materialBomCol);
        this.materialBomColList = materialBomColList;

        var subsidiaryBomColList = tableCol.subsidiaryBomCol;
        subsidiaryBomColList = _.map(subsidiaryBomColList, function (obj) {
          var id = obj.id;
          var checked = config.subsidiaryBomCol.indexOf(id) === -1;
          return {
            id: id,
            label: obj.label,
            checked: checked
          };
        });
        this.setClassifyConfig(subsidiaryBomColList, config.subsidiaryBomCol);
        this.subsidiaryBomColList = subsidiaryBomColList;
      },

      setClassifyConfig: function (bomList, hideColumns) {
        var classifyLevel = BomMaterialFormModel.classifyLevel;
        for (var i = 0; i < classifyLevel.length; i++) {
          var field = classifyLevel[i];
          var label = i18n.getMsg(`bomBusiness.materialClassify.classifyLevel.${field}`);
          var checked = hideColumns.indexOf(field) === -1;
          bomList.push({
            id: field,
            label: label,
            checked: checked
          });
        }
      },

      ensure: function () {
        var materialColorBomColList = this.materialColorBomColList;
        var colorColList = _.filter(materialColorBomColList, {
          checked: false
        });
        if (colorColList.length === materialColorBomColList.length) {
          this.$alert('请选择颜色物料BOM表需要显示的列', '信息');
          return;
        }
        var colorStatus = this.checkMaterialConfig(colorColList);
        if (!colorStatus) {
          this.$alert('颜色物料BOM表物料信息必须要显示一项', '信息');
          return;
        }
        if (!this.checkClassify(colorColList)) {
          this.$alert('颜色物料BOM表选择显示分类，需要选择显示一级分类', '信息');
          return;
        }

        var materialBomColList = this.materialBomColList;
        var materialColList = _.filter(materialBomColList, {
          checked: false
        });
        if (materialColList.length === materialBomColList.length) {
          this.$alert('请选择物料BOM表需要显示的列', '信息');
          return;
        }
        var materialStatus = this.checkMaterialConfig(materialColList);
        if (!materialStatus) {
          this.$alert('物料BOM表物料信息必须要显示一项', '信息');
          return;
        }
        if (!this.checkClassify(materialColList)) {
          this.$alert('物料BOM表选择显示分类，需要选择显示一级分类', '信息');
          return;
        }

        var subsidiaryBomColList = this.subsidiaryBomColList;
        var subColList = _.filter(subsidiaryBomColList, {
          checked: false
        });
        if (subColList.length === subsidiaryBomColList.length) {
          this.$alert('请选择辅料BOM表需要显示的列', '信息');
          return;
        }
        var subsidiaryStatus = this.checkMaterialConfig(subColList);
        if (!subsidiaryStatus) {
          this.$alert('辅料BOM表物料信息必须要显示一项', '信息');
          return;
        }
        if (!this.checkClassify(subColList)) {
          this.$alert('辅料BOM表选择显示分类，需要选择显示一级分类', '信息');
          return;
        }

        var config = {
          materialColorBomCol: _.map(colorColList, 'id'),
          materialBomCol: _.map(materialColList, 'id'),
          subsidiaryBomCol: _.map(subColList, 'id')
        };
        this.$emit('update:config', config);
        this.$emit('setting-success', config);
      },

      checkMaterialConfig: function (list) {
        list = _.map(list, 'id');
        var materialFields = ['materialName', 'materialModel', 'materialProviderName', 'materialColor', 'materialColorDescription'];
        for (var i = 0; i < materialFields.length; i++) {
          var field = materialFields[i];
          if (list.indexOf(field) === -1) {
            return true;
          }
        }
        return false;
      },

      checkClassify: function (list) {
        list = _.map(list, 'id');
        if (list.indexOf('levelRankOne') === -1) {
          return true;
        }
        list = _.filter(list, function (str) {
          return str.indexOf('levelRank') === 0;
        });
        return list.length === 10;
      }
    },

    components: {
      CheckGroup: CheckGroup
    },

    template: template
  };
}();
